package com.snl.micro.blog.user.base.ThreadPool;


import com.snl.micro.blog.user.dao.mapper.UserMapper;
import com.snl.micro.blog.user.pojo.po.UserInfoPo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.concurrent.*;

@Slf4j
@Component
@Data
@AllArgsConstructor

public class ThreadTask {

    @Autowired
    private UserMapper userMapper;
    private ExecutorService executorService;

    public ThreadTask() {
        this.executorService = Executors.newCachedThreadPool();
    }

    private Callable<List<UserInfoPo>> findFans(Long userId) {
        return ()->userMapper.findFans(userId);
    }

    private Callable<List<UserInfoPo>> findFollowings(Long userId) {
        return ()->userMapper.findFollowings(userId);
    }

    public HashMap<String,List<UserInfoPo>> getFansAndFollowings(Long userId) {
        HashMap<String,List<UserInfoPo>> map = new HashMap<>();
        Future<List<UserInfoPo>> fansFuture =
                this.executorService.submit(findFans(userId));

        Future<List<UserInfoPo>> followingsFuture =
                this.executorService.submit(findFollowings(userId));

        try {
            List<UserInfoPo> fans = fansFuture.get();
            List<UserInfoPo> followings = followingsFuture.get();
            map.put("fans",fans);
            map.put("followings",followings);
            return map;
        } catch (InterruptedException | ExecutionException e) {
            throw new RuntimeException(e);
        }
    }
}
